package com.df.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.df.bpm.dto.BusinessLocationsDTO4Subset;
import com.df.bpm.mapper.BusinessLocationsDTO4SubsetMapper;
import com.df.common.utils.MyQueryWrapper4Like;
import com.df.common.exception.BusinessException;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.BusinessLocations;
import com.df.bpm.mapper.BusinessLocationsMapper;
import com.df.bpm.service.BusinessLocationsService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BusinessLocationsServiceImpl extends BaseServiceImpl<BusinessLocationsMapper,BusinessLocations> implements BusinessLocationsService {
	private final BusinessLocationsMapper businessLocationsMapper;
	private final BusinessLocationsDTO4SubsetMapper businessLocationsDTO4SubsetMapper;

	public List<BusinessLocations> getSteps(Integer groupID, Integer superior){
		if(CommonUtils.varIsBlank(groupID)){
			QueryWrapper<BusinessLocations> wrapper=new QueryWrapper<>();
			wrapper.eq("superior", superior)
					.orderByAsc("groupID")
					.orderByAsc("sequence")
					.orderByAsc("entityID")
					.orderByAsc("businessDesc");
			return businessLocationsMapper.selectList(wrapper);
		}
		return businessLocationsMapper.getSteps(groupID,superior);
	}

	public List<Integer> getGroups(Integer superior) {
		return businessLocationsMapper.getGroups(superior);
	}

	public void updateSequences(int length, Integer headSequence, Integer tailSequence, Integer superior, Integer groupID) {
		Integer min = headSequence;
		Integer max = tailSequence;
		if (min > max) {
			min = tailSequence;
			max = headSequence;
		}
		businessLocationsMapper.updateSequences(length, min, max, superior, groupID);
	}
	public void updateSequences(Integer headSequence, Integer tailSequence, Integer superior, Integer groupID) {
		int length=1;
		if(headSequence<tailSequence){
			length=-1;
		}
		Integer min = headSequence;
		Integer max = tailSequence;
		if (min > max) {
			min = tailSequence;
			max = headSequence;
		}
		businessLocationsMapper.updateSequences(length, min, max, superior, groupID);
	}

	/**
	 * 为删除步骤调整其它步骤顺序
	 * @param sequence 目标删除的步骤序号
	 * @param superior	父级业务ID
	 * @param groupID	分组ID
	 */
	public void updateSequences4delete(Integer sequence, Integer superior, Integer groupID){
		businessLocationsMapper.updateSequences4delete(sequence,superior,groupID);
	}

	/**
	 * 删除并重新排序
	 * @param businessLocationsIndex 待删除的目标定位id
	 * @return 删除的行数
	 */
	public Integer deleteAndReSequence(Integer businessLocationsIndex){
		BusinessLocations temp=businessLocationsMapper.selectById(businessLocationsIndex);
		businessLocationsMapper.updateSequences4delete(temp.getSequence(), temp.getSuperior(), temp.getGroupID());
		return businessLocationsMapper.deleteById(businessLocationsIndex);
	}

	/**
	 * 查询目标业务目标分组子级流程列表
	 * @param superior  目标业务id
	 * @param groupId   目标分组id
	 * @return  子集业务流程
	 */
	public List<BusinessLocationsDTO4Subset> getStepDto(Integer superior, Integer groupId) {
		return businessLocationsMapper.getStepDto(superior,groupId);
	}

    public List<BusinessLocationsDTO4Subset> getList4Like(BusinessLocationsDTO4Subset temp) {
		if(CommonUtils.varIsBlank(temp)){
			return null;
		}
		MyQueryWrapper4Like<BusinessLocationsDTO4Subset> wrapper=new MyQueryWrapper4Like<>(temp);
		return businessLocationsDTO4SubsetMapper.selectList(wrapper.stringLike(temp));
    }

	/**
	 * 更改定位，并自动调整相关排序
	 * @param temp 待变更信息
	 */
    public void updateWithAutoSequence(BusinessLocations temp) {
		if(temp.getBusinessLocationsIndex()==null){
			throw new BusinessException("自动调整sequence的定位id不能为空");
		}
		BusinessLocations oldTemp=businessLocationsMapper.selectById(temp.getBusinessLocationsIndex());
		List<BusinessLocations> oldList=businessLocationsMapper.getSteps(oldTemp.getGroupID(), oldTemp.getSuperior());
		if(temp.getGroupID()==null || temp.getGroupID().equals(oldTemp.getGroupID())){//分组不变，单纯调序
			if(temp.getSequence()!=null && !temp.getSequence().equals(oldTemp.getSequence())){//排序变动
				if (temp.getSequence()>oldList.size()){
					temp.setSequence(oldList.size());//超步限自动改成最尾序
				}
				updateSequences(oldTemp.getSequence(),temp.getSequence(),oldTemp.getSuperior(),oldTemp.getGroupID());
			}
		}else{//分组变更
			updateSequences(-1,oldTemp.getSequence(),oldList.size(),
					oldTemp.getSuperior(),oldTemp.getGroupID());//旧分组的后序步骤前移一步

			List<BusinessLocations> newList=businessLocationsMapper.getSteps(temp.getGroupID(), oldTemp.getSuperior());
			if(temp.getSequence()==null){
				temp.setSequence(oldTemp.getSequence());
			}
			if (temp.getSequence()>newList.size()+1){
				temp.setSequence(newList.size()+1);//超步限自动改成最尾序
			}else{//如果不是尾序，把新分组后序步骤后移
				updateSequences(1,temp.getSequence(),newList.size(),oldTemp.getSuperior(),temp.getGroupID());
			}

		}
		businessLocationsMapper.updateById(temp);
	}

	/**
	 * 插入新定位并自动调整相关排序
	 * @param temp 新定位信息
	 */
	public void insertSequence(BusinessLocations temp) {
		if(temp.getSuperior()==null){
			temp.setSuperior(-1);
		}
		if(temp.getGroupID()==null){
			throw new BusinessException("新定位分组不能为空");
		}
		List<BusinessLocations> newList=businessLocationsMapper.getSteps(temp.getGroupID(), temp.getSuperior());
		if(temp.getSequence()==null){
			temp.setSequence(newList.size()+1);
		}
		if(temp.getSequence()<=newList.size()){//后序步骤后移
			updateSequences(1,temp.getSequence(),newList.size(),temp.getSuperior(),temp.getGroupID());
		}
		businessLocationsMapper.insert(temp);
	}
}